package com.blue.firecat.contants;


import com.blue.firecat.enums.ErrorEnum;

import java.io.Serializable;

/**
 * 封装响应对象
 * 作者：ming.guo
 * 时间：2015/8/28 11:33
 */
public class Result<T> implements Serializable {
    /**
     * 错误码，0代表成功，其他代表失败
     */
    private int code = 0;

    /**
     * 错误信息，对应错误码
     */
    private String message;

    /**
     * 数据类型
     */
    private T data;

    public Result() {
        this.message = ErrorEnum.SUCCESS.getValue();
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功
     *
     * @return
     */
    public boolean isSuccess() {
        return this.code == 0;
    }

    /**
     * 失败
     *
     * @return
     */
    public boolean isFail() {
        return this.code != 0;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
        String valueByCode = ErrorEnum.getValueByCode(code);
        if (valueByCode != null) {
            this.setMessage(valueByCode);
        }
    }

    public void setCode(ErrorEnum errorEnum) {
        this.code = errorEnum.getCode();
        String valueByCode = ErrorEnum.getValueByCode(code);
        if (valueByCode != null) {
            this.setMessage(valueByCode);
        }
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
